#include "gamelogi.h"

void gameOver(){
	extern Game game;
	extern Queue keys_queue;
	extern ConfigInfo conf;
	extern int last_sc;
	disable();
	game.gameoverText = create_sprite(gameover_xpm);
	game.gameoverText->x = 20;
	game.gameoverText->y = 100;
	draw_sprite(game.gameoverText,game.gameover->map);
	drawStringAt("Enter para continuar", HRES/2-10*8*4, 300, 16, 0, 4, game.gameover->map, conf.table);
	memcpy(conf.video_base,game.gameover->map,HRES*VRES);
	enable();
	do{
		if(!queueEmpty(&keys_queue))
		{
			last_sc = queueGet(&keys_queue);
		}
	}
	while(last_sc!=KEY_ENTER);
}

void win(){
	extern Game game;
	extern ConfigInfo conf;
	extern char* double_buffer;
	extern int last_sc;
	extern Queue keys_queue;
	int count;
	Score scores[6];
	game.hero.score += map(conf.secActual,0,200,500,0);
	count = importScoresFile(scores);
	qsort(scores,count,sizeof(Score),score_cmp);
	
	initializeMusic(4);
	
	disable();
	game.winText = create_sprite(winCongratz_xpm);
	game.winText->x = 20;
	game.winText->y = 100;
	draw_sprite(game.winText,game.win->map);
	drawStringAt("Space para continuar", HRES/2-10*8*4, 300, 16, 0, 4, game.win->map, conf.table);
	memcpy(conf.video_base,game.win->map,HRES*VRES);
	enable();
	do{
		if(!queueEmpty(&keys_queue))
		{
			last_sc = queueGet(&keys_queue);
		}
	}
	while(last_sc!=KEY_SPACE);
	
	if(scores[count-1].score < game.hero.score){
		int missingLetters = 10;
		char name[11]="";
		do{
			char c;			
			memcpy(double_buffer,game.level1.intro.bg->map,HRES*VRES);
			drawStringAt("Highscore", HRES/2-5*8*8, 25, 16, 0, 8, double_buffer, conf.table);
			drawStringAt("Insira nome:",HRES/2-8*8*4,200,16,0,4,double_buffer,conf.table);
			drawStringAt(name, HRES/2-5*8*4, 300, 16, 0, 4, double_buffer, conf.table);
			
			if(!queueEmpty(&keys_queue))
			{
				last_sc = queueGet(&keys_queue);
				if(last_sc==KEY_BKSP){
					if(missingLetters==10){}
					else{
						missingLetters++;
						name[10-missingLetters]='\0';
					}
				}
				c=scToAscii(last_sc);
				if(c!=0){
					name[10-missingLetters]=c;
					missingLetters--;
				}
			}
			if(missingLetters==0)break;
			memcpy(conf.video_base,double_buffer,HRES*VRES);
		}while(last_sc!=KEY_ENTER);
		Score newscore;
		strcpy(newscore.name,name);
		newscore.score = game.hero.score;
		if(count<5){
			scores[count]=newscore;
			count++;
		}else{
			scores[count-1]=newscore;
		}
		writeScores(scores,count);
	}	
}

void main_loop(){
	extern int last_sc;
	extern Queue keys_queue;
	extern ConfigInfo conf;
	int exit=0;
	do{
		switch(menu()){
			case 0:{
				initGame();
				conf.video_base = enter_graphics(0x103,&(conf.map));
				if(Level1_loop()){
					gameOver();
				}else if(Level2_loop()){
					gameOver();
				}else if(Level3_loop()){
					gameOver();
				}else{
					win();
				}
				break;
			}
			case 1:{
				scoreboard();	
				break;
			}
			case 2:{
				int exitOptions=0;
				do{
					switch(options()){
						case 0:{
							configKeys();
							break;
						}
						case 1:{
							toggleSound();
							break;
						}
						case 2:{
							exitOptions=1;
							break;
						}
					}
				}while(!exitOptions);
				break;
			}
			case 3:{
				exit=1;
				break;
			}
		}
	}while(!exit);
}
